var RepairVisitForm = function () {
  var VisitFormModel = function (visit) {
    BaseModel.call(this);

    var self = this;
    var fn = {
      initData: function (visit) {
        if (visit === undefined) {
          self.visitType = '';
          self.visitPercent = undefined;
          self.status = 1;
          return;
        }

        var fields = ['id', 'visitType', 'status'];
        self.setDataFromObj(visit, fields);

        var visitPercent = visit.visitPercent;
        visitPercent = _.round(visitPercent * 100, 0);
        self.visitPercent = visitPercent;
      }
    };

    this.checkValid = function(){
      var fields = ['visitType', 'visitPercent'];
      return this.checkRequiredFields(fields, true);
    };

    this.getObject = function (type) {
      var postData = {};
      if (type === 'update') {
        postData.id = this.id;
      }

      var fields = ['visitType', 'status'];
      this.setPostData(postData, fields, true, true);

      var visitPercent = this.visitPercent;
      visitPercent = _.round(visitPercent / 100, 2);
      postData.visitPercent = visitPercent;
      return postData;
    }

    fn.initData(visit);
  };

  var template = /* html */ `
    <div class="repair-visit-form" v-loading.fullscreen.lock="loading">
      <el-form label-width="auto">
        <el-form-item label="回访评级">
          <el-input v-if="type==='add'" class="form-input" v-model="visitForm.visitType"></el-input>
          <span v-else>{{visitForm.visitType}}</span>
        </el-form-item>
        <el-form-item label="回访占比">
          <el-input-number style="width:200px" v-model="visitForm.visitPercent" :min="0" :precision="0" controls-position="right">
          </el-input-number>
          <span>%</span>
        </el-form-item>
        <el-form-item label="状态">
          <el-select class="form-input" v-model="visitForm.status">
            <el-option v-for="item in statusList" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div style="text-align:center">
        <el-button type="primary" @click="ensure">
          <span v-if="type==='add'">新增</span>
          <span v-else>修改</span>
        </el-button>
      </div>
    </div>
  `;

  return {
    props: {
      visit: {
        type: Object,
        default: function () {
          return null;
        }
      },

      type: {
        type: String,
        default: 'add'
      }
    },

    data: function () {
      var visitForm;
      if (this.type === 'add') {
        visitForm = new VisitFormModel();
      } else {
        visitForm = new VisitFormModel(this.visit);
      }

      return {
        loading: false,
        visitForm: visitForm,
        statusList: i18n.getDropDown('machineRepair.repairSetting.status', [1, 0])
      };
    },

    methods:{
      ensure:function(){
        var visitForm = this.visitForm;
        var code = visitForm.checkValid();
        if (code !== '') {
          var msg = i18n.getMsg(`machineRepair.repairSetting.error.${code}`);
          this.$alert(msg, '信息');
          return;
        }

        var postData = visitForm.getObject(this.type);
        this.loading = true;
        var self = this;
        ajaxRequest('workOrder/insertOrUpdateWorkOrderVisitType',postData).then(function(msg){
          self.loading = false;
          var msg = self.type === 'add' ? '新增维修回访记录成功' : '修改维修回访记录成功';
          self.$alert(msg, '信息', {
            callback: function () {
              self.$emit('save-success');
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    template: template
  };
}();
